home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / a_to_d / delftips / ti2783.asc next >
Encoding:
Text File  |  1996-09-15  |  8.5 KB  |  366 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2783
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  May 8, 1995                              PAGE  :  1/6
  11.  
  12.     TITLE  :  Creating Database Aliases in Code
  13.  
  14.  
  15.  
  16.  
  17. This Technical Information document will help step thru 
  18. concepts regarding the creation and use of ALIASES within 
  19. your Delphi Applications.
  20.  
  21. Typically, you use the BDE Configuration Utility BDECFG.EXE to
  22. create and configure aliases outside of Delphi.  However, with 
  23. the use of the TDatabase component, you have the ability to 
  24. create and use this ALIAS within your application-- not 
  25. pre-defined in the IDAPI.CFG.
  26.  
  27. The ability to create Aliases that are only available within 
  28. your application is important.  Aliases specify the location 
  29. of database tables and connection parameters for database servers. 
  30. Ultimately, you can gain the advantages of using ALIASES within 
  31. your applications-- without having to worry about the existance 
  32. of a configuration entry in the IDAPI.CFG when you deploy your 
  33. application.  
  34.  
  35. Summary of Examples:
  36. ------- -- ---------
  37. Example #1:  
  38.     Example #1 creates and configures an Alias to use 
  39.     STANDARD (.DB, .DBF) databases.  The Alias is
  40.        then used by a TTable component.
  41. Example #2:
  42.     Example #2 creates and configures an Alias to use
  43.      an INTERBASE database (.gdb).  The Alias is then
  44.     used by a TQuery component to join two tables of
  45.     the database.
  46. Example #3:
  47.     Example #3 creates and configures an Alias to use
  48.         STANDARD (.DB, .DBF) databases.  This example 
  49.     demonstrates how user input can be used to 
  50.     configure the Alias during run-time.
  51.  
  52.  
  53. Example #1:  Use of a .DB or .DBF database (STANDARD)
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2783
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  May 8, 1995                              PAGE  :  2/6
  72.  
  73.     TITLE  :  Creating Database Aliases in Code
  74.  
  75.  
  76.  
  77.  
  78. 1.  Create a New Project.
  79. 2.  Place the following components on the form:
  80.      - TDatabase, TTable, TDataSource, TDBGrid, and TButton 
  81. 3.  Double-click on the TDatabase component or choose Database 
  82.     Editor from the TDatabase SpeedMenu to launch the Database 
  83.     Property editor.
  84. 4.  Set the Database Name to 'MyNewAlias'.  This name will 
  85.     serve as your ALIAS name used in the DatabaseName Property for 
  86.     dataset components such as TTable, TQuery, TStoredProc.
  87. 5.  Select STANDARD as the Driver Name.
  88. 6.  Click on the Defaults Button.  This will automatically add 
  89.     a PATH= in the Parameter Overrides section.
  90. 7.  Set the PATH= to C:\DELPHI\DEMOS\DATA
  91.     (PATH=C:\DELPHI\DEMOS\DATA)
  92. 8.  Click the OK button to close the Database Dialog.
  93. 9.  Set the TTable DatabaseName Property to 'MyNewAlias'.
  94. 10.  Set the TDataSource's DataSet Property to 'Table1'.
  95. 11.  Set the DBGrid's DataSource Property to 'DataSource1'.
  96.  
  97. 12.  Place the following code inside of the TButton's 
  98.      OnClick event.
  99.  
  100.     procedure TForm1.Button1Click(Sender: TObject);
  101.     begin
  102.           Table1.Tablename:= 'CUSTOMER';
  103.           Table1.Active:= True;
  104.     end;
  105.  
  106. 13.  Run the application.
  107.  
  108.  
  109. ***  If you want an alternative way to steps 3 - 11, place the 
  110.       following code inside of the TButton's OnClick event.
  111.  
  112.     procedure TForm1.Button1Click(Sender: TObject);
  113.     begin
  114.           Database1.DatabaseName:= 'MyNewAlias';
  115.           Database1.DriverName:= 'STANDARD';
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   PRODUCT  :  Delphi                                 NUMBER  :  2783
  130.   VERSION  :  All
  131.        OS  :  Windows
  132.      DATE  :  May 8, 1995                              PAGE  :  3/6
  133.  
  134.     TITLE  :  Creating Database Aliases in Code
  135.  
  136.  
  137.  
  138.  
  139.       Database1.Params.Clear;
  140.           Database1.Params.Add('PATH=C:\DELPHI\DEMOS\DATA');
  141.           Table1.DatabaseName:= 'MyNewAlias';
  142.           Table1.TableName:= 'CUSTOMER';
  143.           Table1.Active:= True;
  144.           DataSource1.DataSet:= Table1;
  145.           DBGrid1.DataSource:= DataSource1;
  146.     end;
  147.  
  148. *****
  149.  
  150. Example #2: Use of a INTERBASE database
  151.  
  152. 1.  Create a New Project.
  153. 2.  Place the following components on the form:
  154.      - TDatabase, TQuery, TDataSource, TDBGrid, and TButton 
  155. 3.  Double-click on the TDatabase component or choose Database 
  156.     Editor from the TDatabase SpeedMenu to launch the Database 
  157.     Property editor.
  158. 4.  Set the Database Name to 'MyNewAlias'.  This name will 
  159.     serve as your ALIAS name used in the DatabaseName Property for 
  160.     dataset components such as TTable, TQuery, TStoredProc.
  161. 5.  Select INTRBASE as the Driver Name.
  162. 6.  Click on the Defaults Button.  This will automatically add 
  163.     the following entries in the Parameter Overrides section.
  164.  
  165.     SERVER NAME=IB_SERVER:/PATH/DATABASE.GDB
  166.     USER NAME=MYNAME
  167.     OPEN MODE=READ/WRITE
  168.     SCHEMA CACHE SIZE=8
  169.     LANGDRIVER=
  170.     SQLQRYMODE=
  171.     SQLPASSTHRU MODE=NOT SHARED
  172.     SCHEMA CACHE TIME=-1
  173.     PASSWORD=
  174.  
  175. 7.  Set the following parameters
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.   PRODUCT  :  Delphi                                 NUMBER  :  2783
  191.   VERSION  :  All
  192.        OS  :  Windows
  193.      DATE  :  May 8, 1995                              PAGE  :  4/6
  194.  
  195.     TITLE  :  Creating Database Aliases in Code
  196.  
  197.  
  198.  
  199.  
  200.     SERVER NAME=C:\IBLOCAL\EXAMPLES\EMPLOYEE.GDB
  201.     USER NAME=SYSDBA
  202.     OPEN MODE=READ/WRITE
  203.     SCHEMA CACHE SIZE=8
  204.     LANGDRIVER=
  205.     SQLQRYMODE=
  206.     SQLPASSTHRU MODE=NOT SHARED
  207.     SCHEMA CACHE TIME=-1
  208.     PASSWORD=masterkey
  209.  
  210. 8.  Set the TDatabase LoginPrompt Property to 'False'.  If you 
  211.     supply the PASSWORD in the Parameter Overrides section and set 
  212.     the LoginPrompt to 'False', you will not be prompted for the 
  213.     password when connecting to the database.  WARNING:  If an 
  214.     incorrect password in entered in the Parameter Overrides 
  215.     section and LoginPrompt is set to 'False', you are not prompted 
  216.     by the Password dialog to re-enter a valid password.
  217.  
  218. 9.  Click the OK button to close the Database Dialog.
  219. 10.  Set the TQuery DatabaseName Property to 'MyNewAlias'.
  220. 11.  Set the TDataSource's DataSet Property to 'Query1'.
  221. 12.  Set the DBGrid's DataSource Property to 'DataSource1'.
  222.  
  223. 13.  Place the following code inside of the TButton's 
  224.      OnClick event.
  225.  
  226.     procedure TForm1.Button1Click(Sender: TObject);
  227.     begin
  228.           Query1.SQL.Clear;
  229.           Query1.SQL.ADD(
  230.         'SELECT DISTINCT * FROM CUSTOMER C, SALES S
  231.         WHERE (S.CUST_NO = C.CUST_NO)
  232.         ORDER BY C.CUST_NO, C.CUSTOMER');
  233.           Query1.Active:= True;
  234.     end;
  235.  
  236. 14.  Run the application.
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.   PRODUCT  :  Delphi                                 NUMBER  :  2783
  252.   VERSION  :  All
  253.        OS  :  Windows
  254.      DATE  :  May 8, 1995                              PAGE  :  5/6
  255.  
  256.     TITLE  :  Creating Database Aliases in Code
  257.  
  258.  
  259.  
  260.  
  261. Example #3: User-defined Alias Configuration
  262.  
  263. This example brings up a input dialog and prompts the 
  264. user to enter the directory to which the ALIAS is to 
  265. be configured to.  
  266.  
  267. The directory, servername, path, database name, and other 
  268. neccessary Alias parameters can be read into the 
  269. application from use of an input dialog or .INI file.
  270.  
  271. 1.  Follow the steps (1-11) in Example #1.
  272. 2.  Place the following code inside of the TButton's 
  273.     OnClick event.
  274.  
  275. procedure TForm1.Button1Click(Sender: TObject);
  276. var
  277.   NewString: string;
  278.   ClickedOK: Boolean;
  279. begin
  280.   NewString := 'C:\';
  281.   ClickedOK := InputQuery('Database Path', 
  282.     'Path: --> C:\DELPHI\DEMOS\DATA', NewString);
  283.   if ClickedOK then
  284.   begin
  285.     Database1.DatabaseName:= 'MyNewAlias';
  286.     Database1.DriverName:= 'STANDARD';
  287.     Database1.Params.Clear;
  288.     Database1.Params.Add('Path=' + NewString);
  289.      Table1.DatabaseName:= 'MyNewAlias';
  290.     Table1.TableName:= 'CUSTOMER';
  291.     Table1.Active:= True;
  292.     DataSource1.DataSet:= Table1;
  293.     DBGrid1.DataSource:= DataSource1;
  294.   end;
  295. end;
  296.  
  297. 3.  Run the Application.
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.   PRODUCT  :  Delphi                                 NUMBER  :  2783
  313.   VERSION  :  All
  314.        OS  :  Windows
  315.      DATE  :  May 8, 1995                              PAGE  :  6/6
  316.  
  317.     TITLE  :  Creating Database Aliases in Code
  318.  
  319.  
  320.  
  321.  
  322. -------------------------------------
  323. See Also:
  324.  
  325. Delphi On-line help -->  
  326.     Database Properties Editor
  327.     TDatabase
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362. DISCLAIMER: You have the right to use this technical information
  363. subject to the terms of the No-Nonsense License Statement that
  364. you received with the Borland product to which this information
  365. pertains.
  366.